home *** CD-ROM | disk | FTP | other *** search
/ Disc to the Future 2 / Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin / MAC / THINKC / 3_0 / MDEF__ / PATMENUD.C < prev    next >
C/C++ Source or Header  |  1988-01-02  |  8KB  |  427 lines

  1. #include "patmenudemo.h"
  2.  
  3. /* Globals    */
  4.  
  5. WindowPtr            aWindow;
  6. MenuHandle            AppleMenu;
  7. MenuHandle            FileMenu;
  8. MenuHandle            EditMenu;
  9. MenuHandle            GraphicMenu;
  10. Pattern                testPattern;
  11. EventRecord            theEvent;
  12. Boolean                Finished;
  13. Boolean                quitonwindclose;
  14.  
  15. main()
  16. {
  17.     Init();
  18.     makemenus();
  19.     makewindow();
  20.     Finished = FALSE;
  21.  
  22.     /*    the event loop    */
  23.     while (Finished==FALSE){
  24.         SystemTask();
  25.         if (GetNextEvent(everyEvent,&theEvent))
  26.             handleanevent();
  27.     }
  28.     SetCursor(*(CursHandle)GetCursor(watchCursor));
  29.     zoomport(aWindow,FALSE);
  30. }
  31.  
  32. GracefulExit()
  33. {
  34.     /*
  35.         just return to Finder on system errors
  36.     */
  37.     ExitToShell();
  38. }
  39.  
  40. Init()
  41. {
  42.     int        index;
  43.  
  44.     MaxApplZone();
  45.     for (index=1;index<11;index++)
  46.         MoreMasters();
  47.     InitGraf(&thePort);
  48.     InitFonts();
  49.     FlushEvents(everyEvent,0);
  50.     InitWindows();
  51.     InitMenus();
  52.     TEInit();
  53.     InitDialogs(GracefulExit);
  54.     InitCursor();
  55.     GetIndPattern(&testPattern,MYPATLIST,0);
  56.     quitonwindclose = TRUE;    /* closing application window exits application    */
  57.     randSeed = TickCount();
  58. }
  59.  
  60. abs(x)
  61. int        x;
  62. {
  63.     /*
  64.         this could have been a macro except when used for Random(),
  65.         ie, if it were defined as
  66.  
  67.         #define        abs(x)    (x)>0?(x):-(x)
  68.  
  69.         then abs(Random()) would expand to
  70.         (Random())>0?(Random()):-(Random())
  71.         returning unpredictable results
  72.     */
  73.  
  74.     if (x<0) return (-x);
  75.     else return(x);
  76. }
  77.  
  78. makemenus()
  79. {
  80.     AppleMenu = GetMenu(AppleID);
  81.     AddResMenu(AppleMenu,(ResType)'DRVR');
  82.     InsertMenu(AppleMenu,0);
  83.  
  84.     FileMenu = GetMenu(FileID);
  85.     InsertMenu(FileMenu,0);
  86.  
  87.     EditMenu = GetMenu(EditID);
  88.     InsertMenu(EditMenu,0);
  89.     UpdateEdit(FALSE);
  90.     
  91.     GraphicMenu = NewMenu(GraphicID,"\pPattern");
  92.     /*
  93.         set the MenuHandle.menuProc to the MDEF #130 in our
  94.         resource file
  95.     */
  96.     (*GraphicMenu)->menuProc = GetResource('MDEF',130);
  97.  
  98.     /*
  99.         calculate how large the menu rectangle is
  100.     */
  101.     CalcMenuSize(GraphicMenu);
  102.     InsertMenu(GraphicMenu,0);
  103.  
  104.     DrawMenuBar();
  105. }
  106.  
  107. UpdateEdit(OnOff)
  108. Boolean        OnOff;
  109. {
  110. int        index;
  111.  
  112.     /*
  113.         if OnOff is true, then enable Edit menu items, otherwise
  114.         disable all of the Edit menu items.
  115.     */
  116.     for(index=1;index<7;index++)
  117.         if (OnOff)
  118.             EnableItem(EditMenu,index);
  119.         else
  120.             DisableItem(EditMenu,index);
  121.     DisableItem(EditMenu,2);
  122. }
  123.  
  124. makewindow()
  125. {
  126. Rect        wRect;
  127. FontInfo    FontStuff;
  128.  
  129.     /*
  130.         make an application window
  131.     */
  132.     SetRect(&wRect,0,0,220,150);
  133.     SetPort(aWindow = NewWindow(nil,&wRect,"\pPattern Demo",FALSE,8,(WindowPtr)-1,TRUE,0));
  134.     TextFont(0);
  135.     TextSize(12);
  136.     TextFace(0);
  137.     TextMode(srcCopy);
  138.  
  139.     centerwindow(aWindow,&screenBits.bounds);
  140.     zoomport(aWindow,TRUE);
  141. }
  142.  
  143. odd(theLong)
  144. long    theLong;
  145. {
  146.     /*
  147.         this one could probably have been defined as a macro
  148.     */
  149.     return(theLong % 2);
  150. }
  151.  
  152. DoMenu(mChoice)
  153. long        mChoice;
  154. {
  155.     int            MenuID,MenuItem;
  156.     char        accName[256];
  157.  
  158.     MenuID = HiWord(mChoice);
  159.     MenuItem = LoWord(mChoice);
  160.     switch (MenuID){
  161.         case AppleID:    switch (MenuItem){
  162.                             case 1:        doabout();
  163.                                         break;
  164.  
  165.                             default:    GetItem(AppleMenu,MenuItem,accName);
  166.                                         (void)OpenDeskAcc(accName);
  167.                                         break;
  168.                         }
  169.                         break;
  170.  
  171.         case FileID:    switch (MenuItem){
  172.                             case QuitItem:    Finished = TRUE;
  173.                                             break;
  174.                             default:        break;
  175.                         }
  176.                         break;
  177.  
  178.         case EditID:    if (!SystemEdit(MenuItem-1)){
  179.                             switch(MenuItem){
  180.                                 case UndoItem:        break;
  181.                                 case CutItem:        break;
  182.                                 case CopyItem:        break;
  183.                                 case PasteItem:        break;
  184.                                 case ClearItem:        break;
  185.                                 default:            break;
  186.                             }
  187.                         }
  188.                         break;
  189.  
  190.         case GraphicID:    {
  191.                             GrafPtr        tempPort;
  192.  
  193.                             if (MenuItem > 0){
  194.                                 GetIndPattern(&testPattern,MYPATLIST,MenuItem);
  195.                                 GetPort(&tempPort);
  196.                                 SetPort(aWindow);
  197.                                 InvalRect(&aWindow->portRect);
  198.                                 SetPort(tempPort);
  199.                             }
  200.                         }
  201.                         break;
  202.         }
  203.         HiliteMenu(0);
  204. }
  205.  
  206. DoDrag(aPoint,eventWindow)
  207. Point            aPoint;
  208. WindowPtr        eventWindow;
  209. {
  210.     /*
  211.         drag the application window around within screenBits.bounds
  212.  
  213.         If eventWindow != FrontWindow(), don't bring it to front, this
  214.         way a command-drag can move a background window without
  215.         bringing it to the front, according to the Macintosh user
  216.         interface guidelines
  217.     */
  218.     DragWindow(eventWindow, aPoint, &screenBits.bounds);
  219. }
  220.  
  221. DoContent(aPoint,eventWindow)
  222. Point            aPoint;
  223. WindowPtr        eventWindow;
  224. {
  225. int                    thePart;
  226. register    int        index,pole;
  227. ControlHandle        theControl;
  228. Rect                cRect;
  229.  
  230.     if (FrontWindow() != eventWindow)
  231.         SelectWindow(eventWindow);
  232.     else{
  233.         GlobalToLocal(&aPoint);
  234.         /*
  235.             handle any application-specific mousedown events here
  236.         */
  237.     }
  238. }
  239.  
  240. dogrow(aPoint,eventWindow)
  241. Point            aPoint;
  242. WindowPtr        eventWindow;
  243. {
  244.     long    newSize;
  245.     int        newWidth,newHeight;
  246.     Rect    limitRect;
  247.  
  248.     if (FrontWindow() != eventWindow)
  249.         SelectWindow(eventWindow);
  250.     else{
  251.         /*
  252.             set a limit rectangle for minimum and maximum window size
  253.         */
  254.         SetRect(&limitRect,100,100,32700,32700);
  255.         newSize = GrowWindow(eventWindow,aPoint,&limitRect);
  256.         if (newSize != nil){
  257.             newWidth = LoWord(newSize);
  258.             newHeight = HiWord(newSize);
  259.             SizeWindow(eventWindow,newWidth,newHeight,TRUE);
  260.             InvalRect(&eventWindow->portRect);
  261.         }
  262.     }
  263. }
  264.  
  265. DoGoAway(aPoint,eventWindow)
  266. Point            aPoint;
  267. WindowPtr        eventWindow;
  268. {
  269.     /*
  270.         if the global Boolean "quitonwindclose" is set to true, then
  271.         closing the application window will also exit the application.
  272.         otherwise, the window just goes away.
  273.     */
  274.  
  275.     if (TrackGoAway(eventWindow,aPoint))
  276.         Finished = quitonwindclose;
  277. }
  278.  
  279. handlemousedown()
  280. {
  281.     Point            mouseWhere;
  282.     WindowPtr    whichWindow;
  283.     int                Part;
  284.  
  285.     /*
  286.         we have a mousedown event, find out where the mouse was
  287.         clicked, and dispatch to proper mousedown handling routines
  288.     */
  289.     switch (Part = FindWindow(theEvent.where,&whichWindow)){
  290.             case inSysWindow:    SystemClick(&theEvent,whichWindow);
  291.                                 break;
  292.  
  293.             case inMenuBar:        DoMenu(MenuSelect(theEvent.where));
  294.                                 break;
  295.  
  296.             case inDrag:        DoDrag(theEvent.where,whichWindow);
  297.                                 break;
  298.  
  299.             case inContent:        DoContent(theEvent.where,whichWindow);
  300.                                 break;
  301.  
  302.             case inGrow:        dogrow(theEvent.where,whichWindow);
  303.                                 break;
  304.  
  305.             case inGoAway:        DoGoAway(&theEvent.where,whichWindow);
  306.                                 break;
  307.  
  308.             case inZoomIn:
  309.             case inZoomOut:
  310.                                 if (TrackBox(whichWindow,theEvent.where,Part)){
  311.                                     ZoomWindow(whichWindow, Part, TRUE);
  312.                                     InvalRect(&whichWindow->portRect);
  313.                                 }
  314.                                 break;
  315.  
  316.             default:            break;
  317.     }
  318. }
  319.  
  320. handlekeydown()
  321. {
  322.     char    ch;
  323.     int        chCode;
  324.     long    menuChoice,newTime;
  325.     Rect    charRect;
  326.  
  327.     ch = (char)theEvent.message & charCodeMask;
  328.  
  329.     /*
  330.         was the command-key down?  If so, it might be a command key
  331.         equivalent for a menu item
  332.     */
  333.     if ((theEvent.modifiers & cmdKey) !=0)
  334.         DoMenu(MenuKey(ch));
  335.     else{
  336.         /* application-dependent key presses handled here */
  337.     }
  338.  
  339. }
  340.  
  341. handleactivate()
  342. {
  343.     WindowPtr        eventWindow;
  344.  
  345.     eventWindow = (WindowPtr)theEvent.message;
  346.     SetPort(eventWindow);
  347.  
  348.     if (eventWindow==aWindow){
  349.         DrawGrowIcon(eventWindow);
  350.         if (BitAnd(theEvent.modifiers,activeFlag)!=0){
  351.             /*
  352.                 our window just got an activate event
  353.             */
  354.             if (BitAnd(theEvent.modifiers,changeFlag) !=0)
  355.                 UpdateEdit(FALSE);
  356.         }
  357.         else{
  358.             /*
  359.                 our window just got a deactivate event
  360.             */
  361.             if (BitAnd(theEvent.modifiers,changeFlag) !=0)
  362.                 UpdateEdit(TRUE);
  363.         }
  364.     }
  365. }
  366.  
  367. handleupdate()
  368. {
  369.     GrafPtr        tempPort,thisPort;
  370.     Rect            dummyRect;
  371.  
  372.     GetPort(&tempPort);
  373.     thisPort = (WindowPtr)theEvent.message;
  374.     SetPort(thisPort);
  375.  
  376.     if (thisPort==aWindow){
  377.             BeginUpdate(thisPort);
  378.             EraseRect(&thisPort->portRect);
  379.             DrawGrowIcon(thisPort);
  380.  
  381.             /*
  382.                 clip drawing to exclude the scroll bar areas
  383.                 since our window has a growicon
  384.             */
  385.             dummyRect = thisPort->portRect;
  386.             dummyRect.right -= 15;
  387.             dummyRect.bottom -= 15;
  388.             ClipRect(&dummyRect);
  389.  
  390.             /*
  391.                 our application-specific window drawing routines
  392.             */
  393.             FillRect(&thisPort->portRect,testPattern);
  394.  
  395.             /*
  396.                 after drawing, set the clip region to an
  397.                 arbitrarily large rectangle
  398.             */
  399.             SetRect(&dummyRect,-32000,-32000,32000,32000);
  400.             ClipRect(&dummyRect);
  401.  
  402.             EndUpdate(thisPort);
  403.     }
  404.  
  405.     SetPort(tempPort);
  406. }
  407.  
  408. handleanevent()
  409. {
  410.     /*
  411.         find out what kind of event occured, and dispatch to the
  412.         proper event-handling routines
  413.     */
  414.     switch (theEvent.what){
  415.         case mouseDown:        handlemousedown();
  416.                             break;
  417.         case keyDown:
  418.         case autoKey:        handlekeydown();
  419.                             break;
  420.         case activateEvt:    handleactivate();
  421.                             break;
  422.         case updateEvt:        handleupdate();
  423.                             break;
  424.     }
  425. }
  426.  
  427.